﻿using System;
using System.Windows.Forms;
using AssettDatabase.DataAccess;

namespace AssettDatabase
{
    public partial class Form_Setup : Form
    {
        public Form_Setup()
        {
            InitializeComponent();
        }

        private void Btn_Return_Main_Click(object sender, EventArgs e)
        {
            this.Dispose();
            FormProvider.MainMenu.Show();
        }

        private void Form_Setup_Load(object sender, EventArgs e)
        {
            DAO mDAO = new DAO();
            mDAO.GetConnectionString();
            SU_TB_Database_Name.Text = mDAO.DataBase;
            SU_TB_SQL_INSTANCE.Text = mDAO.Server;
            SU_TB_User.Text = mDAO.User;
            SU_TB_Password.Text = mDAO.Pwd;
            SU_TB_File_Path.Text = mDAO.FilePath;
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveParameters();
            MessageBox.Show("Settings saved OK!. You have to open Asset Database " + Environment.NewLine + "again in order to update connection settings.");
            Application.Exit();
        }

        private void SaveParameters()
        {
            DAO mDAO = new DAO();
            mDAO.DataBase = SU_TB_Database_Name.Text;
            mDAO.Server = SU_TB_SQL_INSTANCE.Text;
            mDAO.User = SU_TB_User.Text;
            mDAO.Pwd = SU_TB_Password.Text;
            mDAO.FilePath = SU_TB_File_Path.Text;
            mDAO.FirstRun = "0";
            mDAO.SetConnectionString();
        }

        private void Form_Setup_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            FormProvider.MainMenu.Show();
        }

        private void Btn_CreateDB_Click(object sender, EventArgs e)
        {
            if (SU_TB_SQL_INSTANCE.Text.Length == 0)
            {
                MessageBox.Show("SQL Instance is a mandatory field");
                return;
            }
            if (SU_TB_Database_Name.Text.Length == 0)
            {
                MessageBox.Show("Database Name is a mandatory field");
                return;
            }
            if (SU_TB_User.Text.Length == 0)
            {
                MessageBox.Show("User is a mandatory field");
                return;
            }
            if (SU_TB_Password.Text.Length == 0)
            {
                MessageBox.Show("Password is a mandatory field");
                return;
            }

            DialogResult dialog = MessageBox.Show("If the database already exists, it will be deleted and created again. Do you want to continue?", "Confirmation", MessageBoxButtons.YesNo);
            if (dialog == DialogResult.Yes)
            {
                SaveParameters();
                DAO mDAO = new DAO();
                mDAO.CreateSchema(SU_TB_SQL_INSTANCE.Text.Trim(), SU_TB_User.Text.Trim(), SU_TB_Password.Text.Trim(), SU_TB_Database_Name.Text.Trim());

                MessageBox.Show("Database created succesfully!");
            }
        }
    }
}
